package com.lvgang.springbootrabbitmq.routing;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lvgang
 */
@Configuration
public class RabbitRoutingConfig {

    private static Logger logger = LoggerFactory.getLogger(RabbitRoutingConfig.class);

    /**
     * Queue 可以有4个参数
     *      1.队列名
     *      2.durable       持久化消息队列 ,rabbitmq重启的时候不需要创建新的队列 默认true
     *      3.auto-delete   表示消息队列没有在使用时将被自动删除 默认是false
     *      4.exclusive     表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean
    public Queue createRoutingQueueA() {
        logger.info("创建RoutingQueueA成功");
        return new Queue(RabbitRoutingConstant.QUEUQ_A,true);
    }
    @Bean
    public Queue createRoutingQueueB() {
        logger.info("创建RoutingQueueB成功");
        return new Queue(RabbitRoutingConstant.QUEUQ_B,true);
    }

    @Bean
    public DirectExchange routingExchange() {
        //配置广播路由器
        logger.info("创建routingExchange成功");
        return new DirectExchange(RabbitRoutingConstant.EXCHANGE);
    }

    @Bean
    public Binding bingQueueAToRoutingExchange() {
        logger.info("绑定RoutingQueueA到routingExchange成功");
        return  BindingBuilder.bind(createRoutingQueueA()).to(routingExchange()).with(RabbitRoutingConstant.QUEUE_KEY_A);
    }

    @Bean
    public Binding bingQueueBToRoutingExchange() {
        logger.info("绑定RoutingQueueA到routingExchange成功");
        return  BindingBuilder.bind(createRoutingQueueB()).to(routingExchange()).with(RabbitRoutingConstant.QUEUE_KEY_B);
    }

}
